package org.infodavid.common.persistence;

import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Version;

import org.hibernate.annotations.Type;
import org.infodavid.common.model.impl.AbstractDataObjectImpl;
import org.joda.time.DateTime;

/**
 * The Class DataStub.
 */
@Entity(name = "DataStub")
@Table(name = "TEST_DATA")
@NamedQueries(value = {
    @NamedQuery(name = "findAll", query = "select td from DataStub td"),
    @NamedQuery(name = "findArchived", query = "select td from DataStub td where td.archived = :archived"),
    @NamedQuery(name = "selectCount", query = "select count(*) from DataStub td"),
    @NamedQuery(name = "deleteAll", query = "delete from DataStub td"),
    @NamedQuery(name = "archiveAll", query = "update DataStub td set archived = 1")
})
public class DataStub extends AbstractDataObjectImpl<Long> implements ILockable {

  /** serialVersionUID. */
  private static final long serialVersionUID = -8051635790815278487L;

  /** The Constant QRY_FIND_ALL. */
  public static final String QRY_FIND_ALL = "findAll";

  /** The Constant QRY_FIND_ARCHIVED. */
  public static final String QRY_FIND_ARCHIVED = "findArchived";

  /** The uuid. */
  private String uuid;

  /**
   * The Constructor.
   * @throws Exception the exception
   */
  public DataStub() throws Exception {
    super();

    uuid = UUID.randomUUID().toString();
  }

  /**
   * The Constructor.
   * @param keyArg the key argument
   * @throws Exception the exception
   */
  public DataStub(final Long keyArg) throws Exception {
    super(keyArg);
  }

  /*
   * (non-Javadoc)
   * @see org.infodavid.common.model.impl.DataObjectImpl#getCreationDate()
   */
  @Override
  @Column(name = "CREATION")
  @Type(type = "org.infodavid.persistence.impl.hibernate.usertype.DateTimeUserType")
  public DateTime getCreationDate() {
    return super.getCreationDate();
  }

  /*
   * (non-Javadoc)
   * @see org.infodavid.common.model.impl.DataObjectImpl#getModificationDate()
   */
  @Override
  @Version
  @Column(name = "MODIFICATION")
  @Type(type = "org.infodavid.persistence.impl.hibernate.usertype.DateTimeUserType")
  public DateTime getModificationDate() {
    return super.getModificationDate();
  }

  /*
   * (non-Javadoc)
   * @see org.infodavid.common.model.impl.DataObjectImpl#getKey()
   */
  @Override
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "ID", nullable = false, unique = true)
  public Long getKey() {
    return super.getKey();
  }

  /**
   * Gets the uuid.
   * @return the uuid
   */
  @Column(name = "UUID")
  public String getUuid() {
    return uuid;
  }

  /**
   * Sets the uuid.
   * @param value the new uuid
   */
  public void setUuid(final String value) {
    uuid = value;
  }

  /*
   * See super class or interface. (non-Javadoc)
   * @see org.infodavid.common.model.impl.DataObjectImpl#isArchived()
   */
  @Override
  @Column(name = "ARCHIVED")
  public boolean isArchived() {
    return super.isArchived();
  }

  /*
   * (non-Javadoc)
   * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
   */
  @Override
  public int hashCode() {
    final int prime = 31;
    final int result = 1;

    return prime * result + (getUuid() == null ? 0 : getUuid().hashCode());
  }

  /*
   * (non-Javadoc)
   * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
   */
  @Override
  public boolean equals(final Object obj) {
    if (this == obj) {
      return true;
    }

    if (obj == null) {
      return false;
    }

    if (!(obj instanceof DataStub)) {
      return false;
    }

    final DataStub other = (DataStub)obj;

    if (getUuid() == null) {
      if (other.getUuid() != null) {
        return false;
      }
    }
    else if (!getUuid().equals(other.getUuid())) {
      return false;
    }

    return true;
  }
}
